﻿Public Class MainWinForm
    Private foodLocation As Point = New Point(10, 10)
    Private snakeDirection As Point = New Point(1, 0) ' 初始方向向右
    Dim generateFoodRand As New Random
    Dim random As New Random()
    Private snakeBody As New List(Of Point) From {New Point(random.Next(1, 40), random.Next(1, 40))}

    Private Sub aboutSubMenuItem_Click(sender As Object, e As EventArgs) Handles aboutSubMenuItem.Click
        AboutForm.ShowDialog()
    End Sub

    Private Sub MainWinForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        '在窗体加载并显示给用户看时，随机出一个食物的地址
        foodLocation = New Point(random.Next(1, 40), random.Next(1, 40))
    End Sub

    Private Sub LeftPanel_Paint(sender As Object, e As PaintEventArgs) Handles LeftPanel.Paint
        ' 绘制蛇
        For Each part In snakeBody
            e.Graphics.FillRectangle(Brushes.Black, part.X * 10, part.Y * 10, 10, 10)
        Next
        '绘制食物
        e.Graphics.FillRectangle(Brushes.Red, foodLocation.X * 10, foodLocation.Y * 10, 10, 10)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles StartGame.Click
        '点击按钮后，启动了timer计时器控件。
        Timer1.Start()
        '让左侧面板获得焦点
        LeftPanel.TabIndex = 0
        LeftPanel.Select()

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        '底部状态栏 分数标签控件的更新，更新分数
        scoreLabel.Text = (snakeBody.LongCount - 1) * 100
        ' 移动蛇
        '把蛇身体列表的最后一个元素拿出来做蛇的头部
        Dim head As Point = snakeBody.Last
        Dim newHead As Point = New Point(head.X + snakeDirection.X, head.Y + snakeDirection.Y)
        '把新头部添加到蛇的身体列表里面
        snakeBody.Add(newHead)
        '更新标签
        Label1.Text = newHead.X
        Label2.Text = newHead.Y
        Label3.Text = foodLocation.X
        Label4.Text = foodLocation.Y

        '判断是否越界 是否跑出左侧边界
        If newHead.X = 0 Then
            snakeDirection = New Point(0, -1)
        End If
        '判断是否越界 是否跑出上边界
        If newHead.Y = 0 Then
            snakeDirection = New Point(1, 0)
        End If
        '判断是否越界 是否跑出右侧边界
        If newHead.X = 45 Then
            snakeDirection = New Point(0, 1)
        End If
        '判断是否越界 是否跑出底部边界
        If newHead.Y = 38 Then
            snakeDirection = New Point(-1, 0)
        End If
        '判断是否越界 是否跑出左边底部边界
        If newHead.Y = 38 And newHead.X = 0 Then
            snakeDirection = New Point(0, -1)
        End If

        ' 检测是否吃到食物
        If newHead.X = foodLocation.X And newHead.Y = foodLocation.Y Then
            ' 随机生成新食物位置
            foodLocation = New Point(random.Next(1, 45), random.Next(1, 40))
        Else
            '把蛇身体的头部给移除
            snakeBody.RemoveAt(0)
        End If
        ' 绘制
        Refresh()
    End Sub

    Private Sub LeftPanel_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles LeftPanel.PreviewKeyDown
        Select Case e.KeyCode
             '处理键盘的W S A D 键
            Case Keys.W
                If snakeDirection.Y <> 1 Then snakeDirection = New Point(0, -1)
            Case Keys.S
                If snakeDirection.Y <> -1 Then snakeDirection = New Point(0, 1)
            Case Keys.A
                If snakeDirection.X <> 1 Then snakeDirection = New Point(-1, 0)
            Case Keys.D
                If snakeDirection.X <> -1 Then snakeDirection = New Point(1, 0)
             '处理键盘的上下左右键
            Case Keys.Up
                If snakeDirection.Y <> 1 Then snakeDirection = New Point(0, -1)
            Case Keys.Down
                If snakeDirection.Y <> -1 Then snakeDirection = New Point(0, 1)
            Case Keys.Left
                If snakeDirection.X <> 1 Then snakeDirection = New Point(-1, 0)
            Case Keys.Right
                If snakeDirection.X <> -1 Then snakeDirection = New Point(1, 0)
        End Select
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles 退出ToolStripMenuItem.Click
        End
    End Sub
End Class


